package autoorder.zsxmlv.component;

import autoorder.zsxmlv.mapper.WechatUserMapper;
import autoorder.zsxmlv.po.Account;
import autoorder.zsxmlv.po.Goods;
import autoorder.zsxmlv.po.Order;
import autoorder.zsxmlv.po.WechatUser;
import autoorder.zsxmlv.service.WechatUserService;
import common.zsxmlv.utils.HttpUtils;
import common.zsxmlv.utils.PropertiesUtil;
import common.zsxmlv.utils.Utils;
import common.zsxmlv.utils.WeiXinWorkUtils;
import mp.weixin.vo.MpPush;
import mp.weixin.vo.TemplateData;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import work.weixin.qq.vo.image.Image;
import work.weixin.qq.vo.image.ImageMessage;
import work.weixin.qq.vo.mpnews.Articles;
import work.weixin.qq.vo.text.Textcard;
import work.weixin.qq.vo.text.TextcardMessage;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.TreeMap;

/**
 * @Author: ZSXM
 * @Description: 订单下单成功后的消息推送组件
 * @Company: DingZheng
 * @Date: Create by 2018/6/10 9:53
 * @Version: 1.0
 * @Modify by:
 */
@Component
@Service
public class PushMsgComponent {

    /**
     * 公众号推送模板消息
     *
     * @param order 订单
     * @param goods 商品
     * @param acct  下单账号
     */
    public static void pushTempletMessage(Order order, Goods goods, Account acct, List<WechatUser> list) {
        String accessToken = MpPush.getAccessToken();
        System.out.println("accessToken==");
        System.out.println(accessToken);
        try {
            String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;
            if (list != null && list.size() > 0) {
                for (WechatUser user : list) {
                    String orderNum = order.getQrCode().substring(order.getQrCode().lastIndexOf('=') + 1);
                    TemplateData data = new TemplateData();
                    data.setTouser(user.getOpenId());
                    data.setTemplate_id("XFfBUYysogAa7nErW69rORusqxPYvhsq_A9s2QFLUMw");
                    data.setUrl("https://ibsbjstar.ccb.com.cn/CCBIS/QR?QRCODE=" + orderNum);
                    TreeMap<String, String> first = TemplateData.item("您好，你已成功下单该商品，请付款", "#672323");
                    TreeMap<String, String> orderNo = TemplateData.item(order.getOrderNum(), "#0000FF");
                    TreeMap<String, String> goodsName = TemplateData.item(goods.getGoodsName(), "#0000FF");
                    TreeMap<String, String> goodsPrice = TemplateData.item("￥" + order.getPayMoney().setScale(2,BigDecimal.ROUND_HALF_UP).toString() + "元", "#0000FF");
                    TreeMap<String, String> payTime = TemplateData.item(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()), "#0000FF");
                    TreeMap<String, String> account = TemplateData.item(acct.getCustname(), "#0000FF");
                    TreeMap<String, String> remark = TemplateData.item("请尽快支付,点击查看详情", "#007300");
                    TreeMap<String, TreeMap<String, String>> map = new TreeMap<>();
                    map.put("first", first);
                    map.put("orderNum", orderNo);
                    map.put("goodsName", goodsName);
                    map.put("goodsPrice", goodsPrice);
                    map.put("payTime", payTime);
                    map.put("account", account);
                    map.put("remark", remark);
                    data.setData(map);

                    JSONObject jsonObject = JSONObject.fromObject(data);
                    String post = null;

                    post = HttpUtils.post(url, jsonObject.toString());

                    System.out.println(post);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();

        }
    }


    public static int pushTextCardMessage(Order order, Goods goods, Account account) throws Exception {
        int code = 0;
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + WeiXinWorkUtils.getAccessToken();
        Textcard textcard = new Textcard();
        textcard.setTitle("支付下单通知");
//        textcard.setDescription("<div class=\"gray\">账号:" + account.getCustname()
//                + "</div> <div class=\"normal\">商品：" + goods.getGoodsName() + " 成功下单,订单金额：" + order.getGoodsPrice() + "</div>" +
//                "<div class=\"highlight\">请您尽快在5分钟内支付\n下单时间：" + Utils.getTime() + "</div>");
        textcard.setDescription("<div class=\"gray\">账号:" + "ACCOUNT"
                + "</div> <div class=\"normal\">商品：" + "goods name" + " 成功下单,订单金额：" + "$8.88" + "</div>" +
                "<div class=\"highlight\">请您尽快在5分钟内支付\n下单时间：" + Utils.getTime() + "</div>");
//        textcard.setUrl(PropertiesUtil.getValue("auto_order.properties", "payUrl") + order.getId());
        textcard.setUrl("https://ibsbjstar.ccb.com.cn/CCBIS/QR?QRCODE=9980005466759533795678");
        TextcardMessage textcardMessage = new TextcardMessage();
        textcardMessage.setAgentid("1000002");
//        textcardMessage.setTouser(account.getUsername());
        textcardMessage.setTouser("name");
        textcardMessage.setToparty("1");
        textcardMessage.setMsgtype("textcard");
        textcardMessage.setTextcard(textcard);
        JSONObject jsonObject = JSONObject.fromObject(textcardMessage);
        String post = HttpUtils.post(url, jsonObject.toString());
        if (post != null && !"".equals(post)) {
            JSONObject object = JSONObject.fromObject(post);
            String errmsg = object.get("errmsg").toString();
            if ("ok".equals(errmsg)) {
                code = 1;
            }
        }
        return code;
    }

    public static int pushImageMessage(String media_id) throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + WeiXinWorkUtils.getAccessToken();
        Image image = new Image();
        image.setMedia_id(media_id);
        ImageMessage imageMessage = new ImageMessage(image);
        imageMessage.setAgentid("1000002");
        imageMessage.setTouser("ZhangSi");
//        imageMessage.setToparty("1");
        imageMessage.setMsgtype("image");
        JSONObject jsonObject = JSONObject.fromObject(imageMessage);
        String post = HttpUtils.post(url, jsonObject.toString());
        System.out.println(post);
        return 0;
    }

    /**
     * 推送图文消息
     *
     * @param order
     * @param goods
     * @param account
     * @param MEDIA_ID
     * @return
     */
    public static int pushMpnewsMessage(Order order, Goods goods, Account account, String MEDIA_ID) {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + WeiXinWorkUtils.getAccessToken();
        Articles articles = new Articles();
        articles.setTitle("下单支付通知");
        articles.setThumb_media_id(MEDIA_ID);
        articles.setAuthor("作者");


        return 0;
    }

    public static void main(String[] args) throws Exception {
        PushMsgComponent.pushTextCardMessage(null, null, null);
    }

}
